package com.xl.bigdata.tools.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

/**
 * @ClassName KafkaConsumerTool
 * @Description 每次都需要初期化
 * @Author xl.gao
 * @Date 2022/2/8 14:50
 * @Version
 */
public class KafkaConsumerSimpleTool {

    public static Properties initKafkaConsumer(String servers, String groupId, int maxPollRecords, boolean commit) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", servers); // "hmaster:9092"
        properties.put("group.id", groupId);
        if (commit) {
            properties.put("enable.auto.commit", "true");
            properties.put("auto.commit.interval.ms", "1000");
        } else {
            properties.put("enable.auto.commit", "false");
        }
        properties.put("enable.auto.commit", "false");
        properties.put("auto.offset.reset", "earliest");  // earliest  latest
        properties.put("session.timeout.ms", "30000");
        properties.put("max.poll.interval.ms", "30000");
        properties.put("max.poll.records", maxPollRecords); // 50
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        return properties;
    }

    public static KafkaConsumer<String, String> getKafkaConsumerOnly(String servers, String topic, String groupId, int maxPollRecords, boolean commit) {


        Properties properties = initKafkaConsumer(servers, groupId, maxPollRecords, commit);
        KafkaConsumer<String, String> kafkaConsumerOnly = new KafkaConsumer<>(properties);
        kafkaConsumerOnly.subscribe(Arrays.asList(topic));
        System.out.println("KafkaConsumer60Client kafkaConsumer初期化完成...");

        return kafkaConsumerOnly;
    }

    public static void main(String[] args) {

        // 获取数据 从60集群的kafka获取数据
        String servers = "10.163.205.94:9092"; //60集群
        String groupId = "out_lecc_work_order_photo_v4_pro_consumer_00001";  // 线上 ： out_lecc_work_order_photo_v4_pro  测试 ：out_lecc_work_order_photo_001_pro_v2_test
        String topic = "rt_yonghu_user_dhzx_information_sheet";
        Properties properties = initKafkaConsumer(servers, topic, 50, false);
        KafkaConsumer<String, String> kafkaConsumerOnly = getKafkaConsumerOnly(servers, topic, groupId, 50, false);

        while (true) {

            ConsumerRecords<String, String> records = kafkaConsumerOnly.poll(1000);


            int count = records.count();

            System.out.println(count);

            records.forEach( c -> {
                ;
                System.out.println(c.value());
            });
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
